import Vue from 'vue';
import Router from 'vue-router';
import login from '@/views/login/login';
import home from "@/views/home/home";
import notFound from '@/views/404/404';
import index from '@/views/index/index';

import ProductClassification from '@/views/Product/ProductClassification';//商品分类
import BargainingProducts from "@/views/Marketing/BargainingProducts";//砍价产品
import FlashKillingProducts from "@/views/Marketing/FlashKillingProducts";//秒杀产品
import GroupProducts from "@/views/Marketing/GroupProducts";//拼团产品
import ShopAssistantList from "@/views/Store/ShopAssistantList";//店员列表
import StoreList from "@/views/Store/StoreList";//门店列表
import DepartmentManagement from "@/views/System/DepartmentManagement";//部门管理
import DictionaryManagement from "@/views/System/DictionaryManagement";//字典管理
import JobManagement from "@/views/System/JobManagement";//岗位管理
import MenuManagement from "@/views/System/MenuManagement";//菜单管理
import RoleManagement from "@/views/System/RoleManagement";//角色管理
import userManagement from "@/views/System/userManagement";//用户管理
import {GetCookie} from '@/config/cookie';

Vue.use(Router)
// 静态路由
let RouterMain = new Router({
  mode: "hash",
  routes: [
    {
      path: '/login',
      name: 'login',
      component: login,
      meta: {
        title: "登录页", progress: true
      }
    },
    {
      path: '*',
      name: 'notFound',
      component: notFound,
      meta: {
        title: "404", progress: true
      }
    },
    {
      path: '/',
      name: 'home',
      component: home,
      redirect: "/index",
      children: [
        {
          path: '/index', name: 'index',
          component: index,
          meta: {title: "首页", icon: '', progress: true}
        }
      ]
    },
    {
      path: '/Product',
      component: home,
      redirect: '/ProductClassification',
      name: 'Product',
      meta: {title: '商品管理', icon: 'icon-shoppingbaggouwudai'},
      children: [
        {
          path: '/ProductClassification',
          name: 'ProductClassification',
          component: ProductClassification,
          meta: {title: '商品分类', icon: 'icon-fenlei'}
        }
      ]
    },
    {
      path: '/Marketing',
      component: home,
      redirect: '/GroupProducts',
      name: 'Marketing',
      meta: {title: '营销管理', icon: 'icon-qushi'},
      children: [
        {
          path: '/GroupProducts',
          name: 'GroupProducts',
          component: GroupProducts,
          meta: {title: '拼团产品', icon: 'icon-duoren'}
        },
        {
          path: '/FlashKillingProducts',
          name: 'FlashKillingProducts',
          component: FlashKillingProducts,
          meta: {title: '秒杀产品', icon: 'icon-miaosha'}
        },
        {
          path: '/BargainingProducts',
          name: 'BargainingProducts',
          component: BargainingProducts,
          meta: {title: '砍价产品', icon: 'icon-Sign'}
        },
      ]
    },
    {
      path: '/Store',
      component: home,
      redirect: '/StoreList',
      name: 'Store',
      meta: {title: '门店管理', icon: 'icon-mendian'},
      children: [
        {
          path: '/StoreList',
          name: 'StoreList',
          component: StoreList,
          meta: {title: '门店列表', icon: 'icon-bianji'}
        },
        {
          path: '/ShopAssistantList',
          name: 'ShopAssistantList',
          component: ShopAssistantList,
          meta: {title: '店员列表', icon: 'icon-duoren'}
        }
      ]
    },
    {
      path: '/System',
      component: home,
      redirect: '/userManagement',
      name: 'System',
      meta: {title: '系统管理', icon: 'icon-shezhi'},
      children: [
        {
          path: '/userManagement',
          name: 'userManagement',
          component: userManagement,
          meta: {title: '用户管理', icon: 'icon-bianji'}
        },
        {
          path: '/RoleManagement',
          name: 'RoleManagement',
          component: RoleManagement,
          meta: {title: '角色管理', icon: 'icon-role'}
        },
        {
          path: '/MenuManagement',
          name: 'MenuManagement',
          component: MenuManagement,
          meta: {title: '菜单管理', icon: 'icon-caidan'}
        },
        {
          path: '/DepartmentManagement',
          name: 'DepartmentManagement',
          component: DepartmentManagement,
          meta: {title: '部门管理', icon: 'icon-bumen'}
        },
        {
          path: '/JobManagement',
          name: 'JobManagement',
          component: JobManagement,
          meta: {title: '岗位管理', icon: 'icon-gongren'}
        },
        {
          path: '/DictionaryManagement',
          name: 'DictionaryManagement',
          component: DictionaryManagement,
          meta: {title: '字典管理', icon: 'icon-zidian'}
        },
      ]
    },
  ]
})

// 路由守卫
RouterMain.beforeEach((to, from, next) => {
  document.title = to.meta.title;
  const isLogin = GetCookie('token') ? true : false;
  if (to.path == '/login') {
    if (isLogin) {
      next('/');
    } else {
      next()
    }
  } else {
    if (isLogin) {
      next();
    } else {
      next('/login');
    }
  }
})
RouterMain.afterEach((to, from) => {

})

// 获取原型对象push函数
const originalPush = Router.prototype.push
// 获取原型对象replace函数
const originalReplace = Router.prototype.replace
// 修改原型对象中的push函数
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
// 修改原型对象中的replace函数
Router.prototype.replace = function replace(location) {
  return originalReplace.call(this, location).catch(err => err)
}

export default RouterMain